package com.xingda.packing.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.common.DateTimeUtils;
import com.xingda.packing.minitor.dto.*;
import com.xingda.packing.minitor.entity.PkAction;
import com.xingda.packing.minitor.entity.PkTask;
import com.xingda.packing.minitor.entity.PkTaskInfo;
import com.xingda.packing.minitor.entity.PkWheel;
import com.xingda.packing.minitor.mapper.PkActionMapper;
import com.xingda.packing.minitor.mapper.PkTaskInfoMapper;
import com.xingda.packing.minitor.mapper.PkTaskMapper;
import com.xingda.packing.minitor.mapper.PkWheelMapper;
import com.xingda.packing.minitor.service.IPkTaskInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@Service
public class PkTaskInfoServiceImpl extends ServiceImpl<PkTaskInfoMapper, PkTaskInfo> implements IPkTaskInfoService {
    @Resource
    private PkTaskMapper pkTaskMapper;
    @Resource
    private PkActionMapper pkActionMapper;
    @Resource
    private PkWheelMapper pkWheelMapper;

    @Override
    public PkClassWheelCountDto getClassWheelCount(String start, String end) {
        end = Objects.isNull(end) ? DateTimeUtils.toStr(new Date()) : end;
        Date sd = DateTimeUtils.toDate(start);
        Date ed = DateTimeUtils.toDate(end);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(ed);
        endCal.add(Calendar.DATE, 1);
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(sd);
        LambdaQueryWrapper<PkTaskInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ge(PkTaskInfo::getPutTime, startCal.getTime()).lt(PkTaskInfo::getPutTime, endCal.getTime())
                .orderByAsc(PkTaskInfo::getPutTime);
        List<PkTaskInfo> list = baseMapper.selectList(queryWrapper);

        List<String> dates = new ArrayList<>();
        List<Long> jia = new ArrayList<>();
        List<Long> yi = new ArrayList<>();
        List<Long> bing = new ArrayList<>();


        while (startCal.getTime().compareTo(endCal.getTime()) < 0) {
            String dateStr = DateTimeUtils.toStr(startCal.getTime());
            dates.add(dateStr);
            List<PkTaskInfo> dayPkTaskInfoList = list.stream().filter(p -> Objects.equals(DateTimeUtils.toStr(p.getPutTime()), dateStr)).collect(Collectors.toList());
            jia.add(dayPkTaskInfoList.stream().filter(p -> p.getPutTime().getHours() < 8).count());
            yi.add(dayPkTaskInfoList.stream().filter(p -> p.getPutTime().getHours() >= 8 && p.getPutTime().getHours() < 16).count());
            bing.add(dayPkTaskInfoList.stream().filter(p -> p.getPutTime().getHours() >= 16).count());
            startCal.add(Calendar.DATE, 1);
        }
        PkClassWheelCountDto classWheelCountDto = new PkClassWheelCountDto();
        classWheelCountDto.setDates(dates);

        PkClassWheelCount jiaClassWheelCount = new PkClassWheelCount();
        jiaClassWheelCount.setName("大夜班");
        jiaClassWheelCount.setDatas(jia);

        PkClassWheelCount yiClassWheelCount = new PkClassWheelCount();
        yiClassWheelCount.setName("白班");
        yiClassWheelCount.setDatas(yi);

        PkClassWheelCount bingClassWheelCount = new PkClassWheelCount();
        bingClassWheelCount.setName("小夜班");
        bingClassWheelCount.setDatas(bing);

        List<PkClassWheelCount> dataList = new ArrayList<>();
        dataList.add(jiaClassWheelCount);
        dataList.add(yiClassWheelCount);
        dataList.add(bingClassWheelCount);
        classWheelCountDto.setDataList(dataList);
        return classWheelCountDto;
    }

    @Override
    public List<WheelCountByRobotDto> getClassWheelCountByRobot(String start, String end) {
        List<WheelCountByRobotDto> list = new ArrayList<>();
        end = Objects.isNull(end) ? DateTimeUtils.toStr(new Date()) : end;
        Date sd = DateTimeUtils.toDate(start);
        Date ed = DateTimeUtils.toDate(end);
        Calendar endCal = Calendar.getInstance();
        if (ed != null) {
            endCal.setTime(ed);
        }
        endCal.add(Calendar.DATE, 1);
        Calendar startCal = Calendar.getInstance();
        if (sd != null) {
            startCal.setTime(sd);
        }
        LambdaQueryWrapper<PkWheel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ge(PkWheel::getScanTime, startCal.getTime()).lt(PkWheel::getScanTime, endCal.getTime())
                .orderByDesc(PkWheel::getScanTime);
        List<PkWheel> pkWheelList = pkWheelMapper.selectList(lambdaQueryWrapper);
        LambdaQueryWrapper<PkAction> actionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        actionLambdaQueryWrapper.ge(PkAction::getStartTime, startCal.getTime()).lt(PkAction::getFinishTime, endCal.getTime())
                .orderByDesc(PkAction::getStartTime);
        if (!pkWheelList.isEmpty()) {
            while (startCal.getTime().compareTo(endCal.getTime()) < 0) {
                WheelCountByRobotDto wheelCountByRobotDto = new WheelCountByRobotDto();
                String dateStr = DateTimeUtils.toStr(startCal.getTime());
                List<PkWheel> dayPkTaskInfoList = pkWheelList.stream().filter(p -> Objects.equals(DateTimeUtils.toStr(p.getScanTime()), dateStr)).collect(Collectors.toList());
                List<WheelCountByRobot> countOne = new ArrayList<>();
                WheelCountByRobot countOneJia = new WheelCountByRobot();
                countOneJia.setName("大夜班");
                countOneJia.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() < 8).filter(p -> p.getArea() == 1).count());
                WheelCountByRobot countOneYi = new WheelCountByRobot();
                countOneYi.setName("白班");
                countOneYi.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() >= 8 && p.getScanTime().getHours() < 16).filter(p -> p.getArea() == 1).count());
                WheelCountByRobot countOneBing = new WheelCountByRobot();
                countOneBing.setName("小夜班");
                countOneBing.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() >= 16).filter(p -> p.getArea() == 1).count());
                WheelCountByRobot countSum1 = new WheelCountByRobot();
                countSum1.setName("运行统计");
                List<WorkDurationResult> workDurationByAreaAndDate1 = pkActionMapper.getWorkDurationByAreaAndDate(1, dateStr);
                countSum1.setCount(Objects.isNull(workDurationByAreaAndDate1.get(0)) ? 0 : workDurationByAreaAndDate1.get(0).getTotalWorkDuration());
                countOne.add(countOneJia);
                countOne.add(countOneYi);
                countOne.add(countOneBing);
                countOne.add(countSum1);
                List<WheelCountByRobot> countTwo = new ArrayList<>();
                WheelCountByRobot countTwoJia = new WheelCountByRobot();
                countTwoJia.setName("大夜班");
                countTwoJia.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() < 8).filter(p -> p.getArea() == 2).count());
                WheelCountByRobot countTwoYi = new WheelCountByRobot();
                countTwoYi.setName("白班");
                countTwoYi.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() >= 8 && p.getScanTime().getHours() < 16).filter(p -> p.getArea() == 2).count());
                WheelCountByRobot countTwoBing = new WheelCountByRobot();
                countTwoBing.setName("小夜班");
                countTwoBing.setCount(dayPkTaskInfoList.stream().filter(p -> p.getScanTime().getHours() >= 16).filter(p -> p.getArea() == 2).count());
                WheelCountByRobot countSum2 = new WheelCountByRobot();
                countSum2.setName("运行统计");
                List<WorkDurationResult> workDurationByAreaAndDate2 = pkActionMapper.getWorkDurationByAreaAndDate(2, dateStr);
                countSum2.setCount(Objects.isNull(workDurationByAreaAndDate2.get(0)) ? 0 : workDurationByAreaAndDate2.get(0).getTotalWorkDuration());
                countTwo.add(countTwoJia);
                countTwo.add(countTwoYi);
                countTwo.add(countTwoBing);
                countTwo.add(countSum2);
                wheelCountByRobotDto.setRobotOne(countOne);
                wheelCountByRobotDto.setRobotTwo(countTwo);
                wheelCountByRobotDto.setDate(dateStr);
                startCal.add(Calendar.DATE, 1);
                list.add(wheelCountByRobotDto);
            }
        }
        Collections.reverse(list);
        return list;
    }

    @Override
    public WheelInfoDto getWheelLocation(String code, PkWheel wheel, PkTaskInfo pkTaskInfo) {
        WheelInfoDto wheelInfoDto = new WheelInfoDto();
        PkTask pkTask = pkTaskMapper.selectById(pkTaskInfo.getTaskId());
        wheelInfoDto.setBarCode(code);
        wheelInfoDto.setLocationInfo("所在层数" + pkTaskInfo.getLayer() + "|" + "位置号" + pkTaskInfo.getPostion());
        wheelInfoDto.setTaskName(pkTask.getTaskName());
        wheelInfoDto.setCustomerName(pkTask.getCustomerName());
        wheelInfoDto.setState(pkTask.getState());
        return wheelInfoDto;
    }
}
